package net.lcqf.tcp.game.match;

import net.lcqf.tcp.game.match.model.MatchPlayer;

import java.util.List;

/**
 * TODO 比赛结束后，根据胜负调整玩家的Elo分数
 * +----------------------------------------------------------------------
 * // | Talk To [ 聊来改善地区金融服务环境，助力企业发展 ]
 * // +----------------------------------------------------------------------
 * // | Copyright (c) 2021-2025 聊来 All rights reserved.
 * // +----------------------------------------------------------------------
 * // | Licensed ( <a href="http://www.apache.org/licenses/LICENSE-2.0">apache</a> )
 * // +----------------------------------------------------------------------
 * // | @Author: 聊来 <18970881148@qq.com>
 * // +----------------------------------------------------------------------
 * // | DateTime: 2025/3/7 17:50
 * // +----------------------------------------------------------------------
 */
public class EloCalculator {

    // K-factor
    private static final int K_FACTOR = 32;

    // 差值比例因子
    private static final int ELO_DIFF_SCALE = 400;


    public static void calculateMatchResult(List<MatchPlayer> teamA, List<MatchPlayer> teamB, boolean aWins) {
        double avgA = teamA.stream().mapToInt(MatchPlayer::getElo).average().orElse(0);
        double avgB = teamB.stream().mapToInt(MatchPlayer::getElo).average().orElse(0);

        double expectedA = 1.0 / (1 + Math.pow(10, (avgB - avgA) / ELO_DIFF_SCALE));
        double expectedB = 1.0 - expectedA;

        int outcomeA = aWins ? 1 : 0;
        int outcomeB = 1 - outcomeA;

        teamA.forEach(p -> {
            int delta = (int) (K_FACTOR * (outcomeA - expectedA));
        });

        teamB.forEach(p -> {
            int delta = (int) (K_FACTOR * (outcomeB - expectedB));
        });
    }
}
